Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

158
Views
How to list "new" member of an JavaScript object that is not instance/static member from its base or inherited class?

I'm not sure how to define this new member thing, please check demo below, what I want is ListNewMemberNames:

The code (demo.html) is assuming running in a browser

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Var1 Test</title>
</head>
<body>
    <script>
        var1 = {value:7}  /* same as window.var1 = {value:7}*/

        ListMemberNames(window) 
        /* returns: ["__defineGetter__", ... "addEventListener", "alert", ... "var1"] */

        ListNewMemberNames(window)
        /* returns: ["var1"] */
        
        function ListMemberNames(obj){ /* tricks with .hasOwnProperty() or something*/ }
        function ListNewMemberNames(obj){ /* ? */ }
    </script>
</body>
</html>
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Generally, you can't analyze the current (or outer) lexical environment (the mapping of variable names to their values) dynamically in JavaScript. This is only possible on the top level due to the fact that top-level variables declared with var (or with nothing at all) get assigned to the window - check the properties that exist on the window at the beginning of pageload, put it into an array, then look at the properties that exist on it later, and find the difference.

// always run this first
const properties = Object.getOwnPropertyNames(window);

// then your code does something
var1 = {value:7}

// and it is detectable by doing:
const newPropertiesOnWindow = Object.getOwnPropertyNames(window)
  .filter(prop => !properties.includes(prop));
console.log(newPropertiesOnWindow);

It's possible to do, barely - but I can't think of any good reason to want to do so.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!